home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: Ben Jones <bjones@eos.hitc.com>
- Newsgroups: comp.lang.c++
- Subject: Re: ?Callbacks / Pointer to member functions?
- Date: Mon, 18 Mar 1996 09:21:43 -0500
- Organization: Hughes Team (EOSDIS)
- Message-ID: <314D7177.6549@eos.hitc.com>
- References: <4i9b3s$7tt@serveru1.naic.wpafb.af.mil>
- NNTP-Posting-Host: lo-mac2134a.hitc.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Macintosh; I; 68K)
-
- jimmie j rohrer/scde/77815 wrote:
- > 1) How can I get a SINGLE pointer to a member function that
- > can be invoked when it is passed to some third party?
- >
- > -or-
- >
- > 2) Is there some clever way to get motif to call any given
- > member function from a 'function' callback?
-
- There is a way to do it. Unfortunately it is compiler and platform
- dependent and the technical details are not typically published. A
- pointer to member function consists of 3 pieces of information encoded
- into 8 bytes (16 on a Dec Alpha):
-
- A flag which indicates if a virtual function is involved.
-
- The address of the actual function, if it is non virtual. The
- offset into the vtable, if it is virtual.
-
- The offset into the object of the address of the vtable.
-
- If you can find out which fields are which, then it is possible to
- obtain the address of the actual function to be called. This
- function will have the same prototype as the member function
- except that it has an additional argument at the beginning of
- the argument list which is the pointer to the object it is to
- operate upon. That is, a member function with the prototype:
-
- type (classname::*)(type1,type2,...)
-
- actually has the prototype:
-
- type (*)(classname*,type1,type2,...)
-
- Which brings to mind the question: Why can't the C++ language
- deal with the expressions:
-
- object.function_name
- pointer->function_name
-
- unless parentheses are applied. It can deal with
-
- function_name
-
- just fine. All it would take would be a way to declare the
- container to hold such an expression. Just as we can say:
-
- type (*pf)(type1,type2,...) = function_name; // Store now
- ...
- pf(arg1,arg2,...) // execute later
-
- a simple extension to C++ would make it possible to say:
-
- type (::*pbmf)(type1,type2,...) = object.function_name;
- ...
- pbmf(arg1,arg2,...)
-
- The <pbmf> (pointer to bound member function) would contain the object
- pointer and the address of the actual function to be applied to that
- object. The compiler could handle overload resolution in this case
- just as easily as it does for the non-member-function case. It could
- generate the appropriate code for obtaining the function address just
- as easily as it does for generating the internal fields of a
- pointer-to-member-function.
-
- COMPILER WRITERS, DO US ALL A BIG FAVOR AND IMPLEMENT THIS, PLEASE.
-
- We could then write much simpler class libraries. Instead of having
- to sub-class in order to override dummy functions, we could simply
- do the intuitively obvious:
-
- a.out = b.in
-
- so that when "out(...)" is called in this instance of class A,
- "b.in(...)" is called.
- --
- Ben Jones
- Hughes Information Technology
- bjones@eos.hitc.com
-